package com.ouyang.service.User;

import com.ouyang.dao.DataBaseDao;
import com.ouyang.dao.User.UserDao;
import com.ouyang.dao.User.UserDaoimpl;
import com.ouyang.poji.User;

import java.sql.Connection;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserServiceImpl () {
        this.userDao = new UserDaoimpl();
    }

    @Override

    public User login ( String UserName, String UserPassword ) {
        Connection connection = null;
        User user = null;
        try {
            connection = DataBaseDao.getConnection();
            user = userDao.getloginuser(connection,UserName);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,null,null);
        }
if(user!=null) {
    if (!user.getUserpassword().equals(UserPassword)) {
        user = null;
    }
}
        return user;
    }

    @Override
    public boolean changepwd ( String UserName, String pwd ) {
        boolean flag = false;
        flag = userDao.changepwd(UserName,pwd);
        if(flag){
            return flag;
        }else {
            return flag;
        }
    }

    @Override
    public List<User> getuserlist ( ) {
        return userDao.getuserlist();
    }

    @Override
    public List<User> search ( String key, String value ) {
        return userDao.search(key,value);
    }

    @Override
    public void add ( String Username, String Password, String Email ) {
        Integer rs = userDao.add(Username, Password, Email);
        if(rs!=1)throw new RuntimeException("添加用户信息失败");
    }

    @Override
    public void updata ( Integer id, String Username, String Password, String Email ) {
        Integer rs = userDao.updata(id,Username,Password,Email);
        if(rs==-1)throw new RuntimeException("修改用户信息失败");
    }

    @Override
    public void del ( String id ) {
        Integer rs = userDao.del(id);
        if(rs==-1)throw new RuntimeException("删除用户信息失败");
    }


}

